from fastapi import APIRouter, Depends
from loguru import logger
from sqlalchemy.orm import Session
from starlette.requests import Request

from apis_module.dao.user_dao import get_user_info
from apis_module.jsonvo.user_vo import UserInfoUpdateRequest
from apis_module.service.user_service import UserService
from config.get_db import get_db
from utils.response_util import *

userMapping = APIRouter()

user_service = UserService()


@userMapping.get("/user/{student_id}")
async def get_user(request: Request, student_id, query_db: Session = Depends(get_db)):
    print(student_id)
    try:
        user = await get_user_info(str(student_id), query_db)
        logger.info('获取成功')

        return response_200(data=user.user_id, message='获取成功')
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))


@userMapping.post("/user/info/update")
def update_user_info(request: Request, user_info: UserInfoUpdateRequest, query_db: Session = Depends(get_db)):
    try:
        result = user_service.update_user_info(query_db, user_info)
        logger.info(f'用户{str(result.user_id)}信息更新成功！')

        return response_200(data=result, message='更新成功')
    except Exception as e:
        logger.exception(e)
        return response_500(data="", message=str(e))
